package cn.bugstack.springframework.support;

import cn.bugstack.springframework.BeansException;
import cn.bugstack.springframework.config.BeanDefinition;

/**
 * @author tyh1
 * beanDefinition 的注册接口
 * 由子类实现具体的获取和创建
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        // 通过父类获取 单例bean
        Object bean = super.getSingleton(beanName);
        if (bean != null) {
            return bean;
        }
        // 如果没获取到，再进行注册创建
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);

    }

    /**
     * 根据bean的名称获取 bean的类信息
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName);

    /**
     * 根据beanName beandefinition 创建bean单例
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition);

}
